package 数组和链表.arraylist;


public class ArrayList  {
    private Object[] L;

    private ArrayList(Object[] L) {
        this.L = L;
    }

   
    public ArrayList InitList(Object[] L) {
        return new ArrayList(L);
    }

   
    public void DestroyList() {
        L = null;
    }

   
    public boolean ListEmpty() {
        if (L.length == 0) {
            return true;
        }
        return false;
    }

   
    public int ListLength() {
        return L.length;
    }

   
    public Object GetElem(int i) {
        if (i < 0 || i >= L.length) {
            throw new IndexOutOfBoundsException("下标越界");
        }
        return L[i];
    }

   
    public int LocateElem(Object e) {
        for (int i = 0; i < L.length; i++) {
            if (L[i].equals(e)) {
                return i;
            }
        }
        return -1;
    }

   
    public Object PirorElem(Object e) {
        int index = LocateElem(e);
        if (index < 1) {
            try {
                throw new Exception("该数没有前驱");
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
        return L[index - 1];
    }

   
    public Object nextElem(Object e) {
        int index = LocateElem(e);
        if (index < 0 || index >= ListLength() - 1) {
            try {
                throw new Exception("该数没有后驱");
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
        return L[index + 1];
    }

   
    public void ListInsert(Object e, int index) {
        if (index < 0 || index > L.length) {
            throw new IndexOutOfBoundsException("下标越界");
        }
        Object[] newL = new Object[L.length + 1];
        if (index == L.length) {
            for (int j = 0; j <= L.length - 1; j++) {
                newL[j] = L[j];
            }
            newL[newL.length - 1] = e;

        } else {
            for (int j = L.length - 1; j >= 0; j--) {
                if (j > index) {
                    newL[j + 1] = L[j];
                } else if (j == index) {
                    newL[j + 1] = L[j];
                    newL[j] = e;

                } else {
                    newL[j] = L[j];
                }
            }
        }
        L = newL;
        newL = null;

    }

   
    public Object ListDelete(int index) {
        if (index < 0 || index >= L.length) {
            throw new IndexOutOfBoundsException("下标越界");
        }
        Object e = L[index];
        Object[] newL = new Object[L.length - 1];
        for (int j = index; j <= L.length - 2; j++) {
            newL[j] = L[j + 1];
        }
        L = newL;
        newL = null;
        return e;
    }

   
    public void ListObjectraverse() {
        for (Object o : L) {
            int e = (int) o;
            System.out.print(o + "\t");
        }
    }

   
    public void Replace(Object e, int index) {
        L[index] = e;
    }

    static class Main {
        public static void main(String[] args) {
            Object[] a = {1, 3, 4, 5, 9};
            Object[] b = {2, 4, 6, 8};
            ArrayList AL = new ArrayList(a);
            ArrayList BL = new ArrayList(b);
            union(AL, BL);
            AL.ListObjectraverse();
            //  mergerListOne(AL, BL).ListObjectraverse();


        }

        private static void union(ArrayList a, ArrayList b) {
            int aLenth = a.ListLength();
            int bLenth = b.ListLength();
            for (int i = 0; i <= bLenth - 1; i++) {
                Object temp = b.GetElem(i);
                if (a.LocateElem(temp) == -1) {
                    a.ListInsert(temp, aLenth++);
                }
            }
        }

        private static ArrayList mergerList(ArrayList a, ArrayList b) {
            // Object[] a = {1, 3, 4, 5, 9};
            // Object[] b = {2, 4, 6, 8};
            int aLength = a.ListLength();
            int bLength = b.ListLength();
            ArrayList c = new ArrayList(new Object[1]);
            int i = 0, j = 0;
            int length = 0;
            boolean flag = false;
            while (i < aLength && j < bLength) {
                if ((int) a.GetElem(i) <= (int) b.GetElem(j)) {
                    if (length == 0 && !flag) {
                        c.Replace(a.GetElem(i), i);

                    } else {
                        c.ListInsert(a.GetElem(i), length++);
                    }
                    flag = true;
                    i++;

                } else {
                    if (length == 0 && !flag) {
                        c.Replace(b.GetElem(j), j);
                    } else {
                        c.ListInsert(b.GetElem(j), length++);
                    }
                    flag = false;
                    j++;
                }
            }
            while (i < aLength) {
                c.ListInsert(a.GetElem(i), length++);
                i++;
            }
            while (j < bLength) {
                c.ListInsert(b.GetElem(j), length++);
                j++;
            }
            return c;
        }
    }

    private static ArrayList mergerListOne(ArrayList a, ArrayList b) {
        // Object[] a = {1, 3, 4, 5, 9};
        // Object[] b = {2, 4, 6, 8};

        int bLength = b.ListLength();
        int j = 0;
        while (j < bLength) {
            int aLength = a.ListLength();
            for (int k = j; k < aLength; k++) {
                Object temp = b.GetElem(j);
                if ((int) temp < (int) a.GetElem(k)) {
                    a.ListInsert(temp, k);
                    j++;
                    break;
                }
            }
        }

        return a;
    }

}
